﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Text;
using System.Drawing;

public partial class ReceiveImg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string fileNm = null;
        //接受图片流

        try
        {
            string filePath = Server.MapPath("shareimages");
            fileNm = System.DateTime.Now.ToString("yyMMddHHmmss") + ".jpg";
            Stream responseStream = Request.InputStream;
            FileStream outputStream = new FileStream(filePath + @"\" + fileNm, FileMode.Create);
            int count = 1024;
            byte[] buffer = new byte[count];
            for (int i = responseStream.Read(buffer, 0, count); i > 0; i = responseStream.Read(buffer, 0, count))
            {
                outputStream.Write(buffer, 0, i);
            }
            outputStream.Close();
            responseStream.Close();
            responseStream.Dispose();
            //加水印


            //Bitmap tmpimage = new System.Drawing.Bitmap(filePath + @"\" + fileNm);

            //Graphics g = Graphics.FromImage(tmpimage);

            //tmpimage.Save(filePath + @"\" + fileNm);      //保存作品图片到服务器
        }
        catch (Exception ex)
        {
            xml(fileNm);
            //Response.Clear();
            //Encoding gb2312 = Encoding.GetEncoding("UTF-8");
            //Response.ContentEncoding = gb2312;
            //XmlDocument doc = new XmlDocument();
            //XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            //doc.AppendChild(decl);

            ////创建根节点   data
            //XmlElement element = doc.CreateElement("root");
            //doc.AppendChild(element);
            //XmlElement success = doc.CreateElement("error");
            //success.InnerText = ex.Message;                   //异常信息

            //element.AppendChild(success);

            ////向页面输出xml文档
            //Response.Write(doc.OuterXml);
            //Response.End();
            
        }
        finally
        {
            xml(fileNm);
        }


    }
    public void xml(string fileNm)
    {
        //生成xml文档
        Response.Clear();
        Encoding gb2312 = Encoding.GetEncoding("UTF-8");
        Response.ContentEncoding = gb2312;
        XmlDocument doc = new XmlDocument();
        XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(decl);

        //创建根节点   data
        XmlElement element = doc.CreateElement("root");
        doc.AppendChild(element);

        //生成success和 pic子节点

        if (fileNm.Length > 0)
        {
            XmlElement success = doc.CreateElement("result");
            success.InnerText = "1";                      //1是成功 0 是失败

            element.AppendChild(success);

            XmlElement pic = doc.CreateElement("pic");
            pic.InnerText = "http://www.lattissima.com.cn/shareimages/" + fileNm;
            element.AppendChild(pic);
        }
        else
        {
            XmlElement success = doc.CreateElement("result");
            success.InnerText = "0";                      //1是成功 0 是失败

            element.AppendChild(success);
        }
        //向页面输出xml文档
        Response.Write(doc.OuterXml);
        Response.End();
    }
}
